﻿using System;
using System.Web.Mvc;
using EnetaMvc.ApplicationServices;
using EnetaMvc.ApplicationServices.Dto;
using EnetaMvc.Web.Attributes;
using EnetaMvc.Web.Models;

namespace EnetaMvc.Web.Controllers
{
    public class BlogsController : BaseController
    {
        private readonly IContentService _contentService;
        private readonly INewsService _newsService;
        private readonly IUserService _userService;

        public BlogsController(IContentService contentService, INewsService newsService, IUserService userService)
        {
            _contentService = contentService;
            _newsService = newsService;
            _userService = userService;
        }

        [HttpGet]
        public ActionResult Details(long id, int page = 1)
        {
            if (page < 1)
                page = 1;

            var model = new BlogDetailsModel();

            if (!User.IsInRole("Administrator"))
            {
                model.Blog = _newsService.GetPublishedBlogById(id);
                model.Posts = _newsService.ListPublishedPostsForBlog(id, page, 15);
            }
            else
            {
                model.Blog = _newsService.GetBlogById(id);
                model.Posts = _newsService.ListPostsForBlog(id, page, 15);
            }
            return View(model);
        }

        [HttpGet]
        [EnetaAuthorize(Roles="User, Administrator")]
        public ActionResult Add()
        {
            var model = new BlogDto();
            
            return View("Edit",model);
        }

        [HttpPost]
        [EnetaAuthorize(Roles = "User, Administrator")]
        public ActionResult Add(BlogDto model)
        {
            return SaveBlog(model);
        }

        [HttpGet]
        [EnetaAuthorize(Roles = "User, Administrator")]
        public ActionResult Edit(long id)
        {
            var blog = _newsService.GetBlogById(id);
            if (blog == null)
                return HttpNotFound("Cannot find blog with ID " + id);

            if(!User.IsInRole("Administrator"))
                if(!_contentService.IsUserContentOwner(User.Identity.Name, id))
                    return new HttpStatusCodeResult(403, "Cannot modify blog");

            return View(blog);
        }

        [HttpPost]
        [EnetaAuthorize(Roles = "User, Administrator")]
        public ActionResult Edit(BlogDto model)
        {
            return SaveBlog(model);
        }

        [NonAction]
        private ActionResult SaveBlog(BlogDto model)
        {
            if (!ModelState.IsValid)
                return View("Edit", model);

            var user = _userService.GetUserByToken(User.Identity.Name);
            var blog = _newsService.GetBlogById(model.Id);
            if (blog != null)
            {
                model.Created = blog.Created;
                model.CreatedById = blog.CreatedById;
            }
            if (model.Id == 0)
            { 
                model.Created = DateTime.Now;
                model.CreatedById = user.Id;
                model.PublishingStateById = user.Id;
                model.PublishingStateDate = model.Created;
                model.PublishingStateId = 2;
            }
            else if (!User.IsInRole("Administrator"))
            {
                if (!_contentService.IsUserContentOwner(User.Identity.Name, model.Id))
                    return new HttpStatusCodeResult(403, "Cannot modify blog");

                if (blog.PublishingStateId == 1)
                    model.PublishingStateId = 1;
                else
                    model.PublishingStateId = 2;

                model.PublishingStateById = user.Id;
                model.PublishingStateComment = string.Empty;
                model.PublishingStateDate = DateTime.Now;
            }
            else
            {
                model.PublishingStateDate = DateTime.Now;
                model.PublishingStateById = user.Id;
            }

            _newsService.SaveBlog(model);

            return RedirectToAction("Details", "Profiles", new { id = user.Id });
        }
    }
}
